package com.sscw.tcp.service.impl;

import com.sscw.tcp.dao.*;
import com.sscw.tcp.entity.*;
import com.sscw.tcp.enums.*;
import com.sscw.tcp.service.BaseUtilService;
import com.sscw.tcp.service.MQTTService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.util.List;
import java.util.Objects;

@Slf4j
@Service
@AllArgsConstructor
public class MQTTServiceImpl implements MQTTService {

    private final GRecordGameDAO gRecordGameDAO;
    private final GConfRoomDAO gConfRoomDAO;
    private final UAppUserEquityDAO uAppUserEquityDAO;
    private final BaseUtilService baseUtilService;
    private final DDetailsPineconeDAO dDetailsPineconeDAO;
    private final MRecordTaskDailyDAO mRecordTaskDailyDAO;
    private final MRecordTaskDAO mRecordTaskDAO;
    private final MRecordOccupyDAO mRecordOccupyDAO;
    private final GRecordMqttMessageDAO gRecordMqttMessageDAO;

    @Override
    @Transactional
    public Boolean dealPushCoins(GRecordMqttMessagePO messagePO) {
        LocalDate now = LocalDate.now();
        GRecordGamePO gamePO = gRecordGameDAO.findById(messagePO.getGameId());
        GConfRoomPO roomPO = gConfRoomDAO.findById(gamePO.getRoomId());
        Integer usePinecone = roomPO.getMultipleValue();
        gRecordGameDAO.updateData(gamePO.getId(), usePinecone, 0, messagePO.getCreateTime());
        if (Objects.isNull(gamePO.getRecordOccupyId())) {
            uAppUserEquityDAO.addPinecone(gamePO.getUserId(), -usePinecone);
            DDetailsPineconePO myDetail = new DDetailsPineconePO();
            myDetail.setUserId(gamePO.getUserId());
            myDetail.setLabelType(LabelTypeEnum.play_pay.getCode());
            myDetail.setDetailsType(DetailsTypeEnum.pay_out.getCode());
            myDetail.setPinecone(usePinecone);
            dDetailsPineconeDAO.save(myDetail);
            String dailyKey = gamePO.getUserId() + now.toString() + "dailyTask";
            String key = gamePO.getUserId() + now.toString() + "task";
            baseUtilService.setDailyTask(dailyKey, gamePO.getUserId(), now);
            baseUtilService.setTask(key, gamePO.getUserId());
            List<MRecordTaskDailyPO> list = mRecordTaskDailyDAO.findUserTodayAction(gamePO.getUserId(), now, DailyTaskActionEnum.total_use_pinecone.getCode());
            if (CollectionUtils.isNotEmpty(list)) {
                list.forEach(po -> {
                    po.setCurrentProgress(Math.addExact(po.getCurrentProgress(), usePinecone));
                    if (po.getCurrentProgress() >= po.getThreshold() && po.getStatus().equals(TaskStatusEnum.go_complete.getCode())) {
                        po.setStatus(TaskStatusEnum.unclaimed.getCode());
                    }
                });
                mRecordTaskDailyDAO.updateBatchById(list);
            }
            baseUtilService.dealDailyTaskAllComplete(gamePO.getUserId(), now);
            List<MRecordTaskPO> pos = mRecordTaskDAO.findList(gamePO.getUserId(), TaskActionEnum.total_use.getCode());
            if (CollectionUtils.isNotEmpty(pos)) {
                pos.forEach(po -> {
                    po.setCurrentProgress(Math.addExact(po.getCurrentProgress(), usePinecone));
                    if (po.getCurrentProgress() >= po.getThreshold() && po.getStatus().equals(TaskStatusEnum.go_complete.getCode())) {
                        po.setStatus(TaskStatusEnum.unclaimed.getCode());
                    }
                });
                mRecordTaskDAO.updateBatchById(pos);
            }
        } else {
            mRecordOccupyDAO.addPinecone(gamePO.getRecordOccupyId(), -usePinecone);
        }
        messagePO.setStatus(Boolean.TRUE);
        gRecordMqttMessageDAO.updateById(messagePO);
        return Boolean.TRUE;
    }

}
